package cn.cvs.servlet;

import cn.cvs.pojo.SysUser;
import cn.cvs.service.SysUserService;
import cn.cvs.util.SpringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(name = "LoginServlet", urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        SysUserService sysUserService= (SysUserService) SpringUtil.getBean ( "sysUserService" );
        String account = request.getParameter ( "account" );
        String password = request.getParameter ( "password" );
        SysUser user = sysUserService.getUserByAccountAndPwd ( account, password );
        if (user!=null){
            request.getSession ().setAttribute ( "userSession",user );
            request.getRequestDispatcher("/WEB-INF/jsp/frame.jsp").forward(request,response);
        }else {
            request.setAttribute ( "error","用户名或密码错误" );
            request.getRequestDispatcher ( "/WEB-INF/jsp/login.jsp" ).forward ( request,response );
        }
    }
}
